<template>
  <div>
    <Header>
    </Header>
    <p style="text-align: center;">{{jumpMessage}}</p>
  </div>
</template>

<script>
import { mapMutations } from "vuex";
import Header from "@/components/header";
import { sessionStorage, getHome } from "@/utils";
import { fetchAccountEnter } from "@/api";

export default {
  components: {
    Header
  },
  data() {
    return {
      jumpMessage: "正在进入账户，请稍等！"
    };
  },
  methods: {
    ...mapMutations(["SET_DYNAMIC_ROUTES"]),

    // 获取登录账户权限
    fetchAccountEnter(userId) {
      fetchAccountEnter({
        query: {
          userId
        }
      }).then(res => {
        let { administrator, advertiser } = res.data;
        sessionStorage.set("ENTER_USER_NAME", advertiser.userName);
        sessionStorage.set("USER", {
          ...advertiser,
          userName: administrator.userName,
          token: administrator.token
        });
        // 没有权限
        if (!advertiser.permission.length) {
          this.jumpMessage = "进入账户未分配权限，请分配权限后重新进入！";
        } else {
          this.SET_DYNAMIC_ROUTES(this.$router);
          this.$router.replace({
            name: getHome()
          });
        }
      });
    }
  },
  beforeRouteEnter(to, from, next) {
    next(vm => {
      let userId = vm.$route.params.secret.slice(8, -8);
      vm.fetchAccountEnter(userId);
    });
  }
};
</script>